<template>
  <div class="detail">
    <el-card>
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="登录账户设置" name="login">
          <el-form :model="form" label-width="120px" :rules="rules" ref="form">
            <el-form-item label="姓名" prop="username">
              <el-input
                placeholder="请输入用户姓名"
                v-model="form.username"
              ></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input
                placeholder="请输入密码"
                v-model="form.password"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submit">更新</el-button>
              <el-button type="primary">取消</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="个人信息" name="userInfo">
            <!-- 用户信息子组件 -->
          <user-info :userInfo="user2"></user-info>
        </el-tab-pane>
        <el-tab-pane label="岗位信息" name="gangwei"></el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import { getUser, sysUserPut } from '@/api/employess'

export default {
  components: {
    userInfo: () => import('./userInfo')
  },
  data () {
    return {
      // tab默认显示
      activeName: 'login',
      id: '',
      user: {},
      user2: {}, //要穿入子组件的数据
      // 表单数据源
      form: {
        username: '',
        password: ''
      },
      // 表单验证
      rules: {
        username: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入姓名', trigger: 'blur' }]
      }
    }
  },
  async created () {
    this.id = this.$route.params.id
    const res = await getUser(this.id)
    this.user = res
    this.user2 = JSON.parse(JSON.stringify(this.user))
    this.form.username = this.user.username
    // console.log(res)
  },
  methods: {
    // 更新
    submit () {
      // 全局验证
      this.$refs.form.validate(async valid => {
        if (valid) {
          const res = await sysUserPut({ ...this.user, ...this.form })
          this.$message.success('修改成功')
          console.log(res)
        }
      })
    },
    // tab切换点击事件
    handleClick () {}
  }
}
</script>

<style></style>
